package cn.utiles;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class JDBCUtiles {
    //获取c3p0的数据源对象
    //zcqd
    private static DataSource dataSource = new ComboPooledDataSource();
    //获取一个连接的方法
    public static Connection getConn(){

        Connection conn = null;
        try {

            conn = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return conn;
//        try {
//            return  dataSource.getConnection();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//            throw new RuntimeException(throwables);
//        }
    }
    //关闭资源的方法
    public static void closeAll(ResultSet rs, Statement stat, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                rs = null;
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                stat = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                conn = null;
            }
        }


    }
    public static void closeAll(PreparedStatement ps, Connection conn){
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                ps = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                conn = null;
            }
        }
    }
    public static void closeAll(PreparedStatement ps, ResultSet rs,Connection conn){
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                ps = null;
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                rs = null;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                conn = null;
            }
        }
    }


}
